var prev_v = "";

// load search results 
function loadResults() {
	// TODO: bug in FF keeps loading results
	v = $('#search-q').attr("value");
	if (v != prev_v) {
		prev_v = v;
		$('#search-status').html("1");
		$('#search-results').html("");
	}
	start = parseInt($('#search-status').html());
	if (v && start > 0 && start < 100) {
		//v = encodeURIComponent(v);
		$.get('index.php', {
			m: "search",
			t: "yt",
			start: start,
			q: v }, function(res) {
				if (res) {
					$('#search-results').append("<div id=\"resultset-"+start+"\">"+res+"</div>");
					$("#resultset-"+start).hide().slideDown(function() {
						// sortable and tooltip
						$("#search-results div.result").draggable({ 
						    helper: "clone" 
						}).tooltip({ 
							delay: 0, 
							showURL: false, 
							track: true,
							fade: 250,
							bodyHandler: function() { 
								return $("<img/>").attr("src", $(this).attr("thumbnail"));
							} 
						});
					});
					$('#search-status').html(start+10);					
				} else {
					$('#search-status').html("-1");
				}
			});
		printLog('Search results for '+v+' loaded...');
	}
}

// add track to playlist
function loadPlaylistEntry(q) {
	video = q;
	if (playlistVidExists(video)) {
		printMsg('Track already in playlist', 1);
	} else {
		$.ajax({
		  type: "POST",
		  url: "index.php?m=playlist&t=add",
		  data: "q="+q,
		  success: function(html){
			playlistAddEntry(html, q);
	   	  }
		});
	}
}